package com.base.vistter.iframe.inverter.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.base.vistter.iframe.bean.Result;
import com.base.vistter.iframe.entity.SysArea;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.service.SysAreaService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/sys/area")
public class SysAreaController {

    @Resource(name = "sysAreaService")
    private SysAreaService sysAreaService;

    @GetMapping
    public Result<List<SysArea>> getList(String code , String name) throws IframeException {
        LambdaQueryWrapper<SysArea> lambdaQueryWrapper = new LambdaQueryWrapper<SysArea>();
        if(StringUtils.isNotEmpty(code)){
            lambdaQueryWrapper.like(SysArea::getCode , code);
        }
        if(StringUtils.isNotEmpty(name)){
            lambdaQueryWrapper.like(SysArea::getName , name);
        }
        return Result.ok(sysAreaService.selectList(lambdaQueryWrapper));
    }

    @PostMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> save(@RequestBody SysArea sysArea) throws IframeException {
        return Result.ok(sysAreaService.save(sysArea));
    }

    @PutMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> update(@RequestBody SysArea sysDictArea) throws IframeException {
        return Result.ok(sysAreaService.update(sysDictArea));
    }

    @DeleteMapping("/{id}")
    public Result<Integer> delete(@PathVariable("id") Integer id) throws IframeException {
        return Result.ok(sysAreaService.delete(id));
    }
    @GetMapping("/tree")
    public Result<List<SysArea>> treeList() throws IframeException {
        return Result.ok(sysAreaService.selectList());
    }

    @GetMapping("/city")
    public Result<List<SysArea>> city() throws IframeException {
        return Result.ok(sysAreaService.city());
    }

    @GetMapping("/text")
    public Result<List<SysArea>> text(@RequestParam("code") String code) throws IframeException{
        return Result.ok(sysAreaService.text(code));
    }
}
